/**
 * Created by python on 18-8-29.
 */
var vm = new Vue({
    el: '#app',
    delimiters: ['[[', ']]'], // 设置vue中的模板语法
    cart: [],
    data: {
        host,
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        cart_total_count: 0, // 购物车总数量
        cart: [], // 购物车数据,
        f1_tab: 1, // 1F 标签页控制
        f2_tab: 1, // 2F 标签页控制
        f3_tab: 1, // 3F 标签页控制
        total_selected_count: 0,
    },
    mounted: function(){
        this.get_cart();
        this.secondKill();

    },
    computed:{
        total_selected_amount: function(){
                    var total = 0;
                    this.total_selected_count = 0;
                    for(var i=0; i<this.cart.length; i++){
                        if(this.cart[i].selected) {
                            total += (parseFloat(this.cart[i].price) * parseFloat(this.cart[i].count));
                            this.total_selected_count += parseInt(this.cart[i].count);
                        }
                    }
                    return total.toFixed(2);
                },
     },
    methods: {
        // 退出
        logout: function(){
            sessionStorage.clear();
            localStorage.clear();
            location.href = '/login.html';
        },
        // 获取购物车数据
        get_cart: function(){
         axios.get(this.host+'/cart/', {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json',
                withCredentials: true
            })
            .then(response => {
                this.cart = response.data;
                for(var i=0; i<this.cart.length; i++){
                    this.cart_total_count = i+1;
                    this.cart[i].url = '/goods/' + this.cart[i].id + '.html';
                }


            })
            .catch(error => {
                console.log(error.response.data);
            })
        },

             /*秒杀倒计时*/
        secondKill : function() {
            axios.get(this.host+'/skus/secondkill/', {
                    responseType: 'json'
                })
                .then(response => {
                    var time_second = response.data;
                            /*复盒子*/
                    var parentTime = document.getElementsByClassName('ms_time')[0];
                    /*span时间*/
                    var timeList = parentTime.getElementsByClassName('num');
                    console.log(timeList.length);
                    var times = time_second;
                    var timer;
                    timer = setInterval(function () {
                    times--;

                    var h = Math.floor(times / (60 * 60));
                    var m = Math.floor(times / 60 % 60);
                    var s = times % 60;

                    //console.log(h + '-' + m + "-" + s);

                    timeList[0].innerHTML = h > 10 ? Math.floor(h / 10) : 0;
                    timeList[1].innerHTML = h % 10;

                    timeList[2].innerHTML = m > 10 ? Math.floor(m / 10) : 0;
                    timeList[3].innerHTML = m % 10;

                    timeList[4].innerHTML = s > 10 ? Math.floor(s / 10) : 0;
                    timeList[5].innerHTML = s % 10;
                    if (times <= 0) {
                        clearInterval(timer);
                            }
                        }, 1000);

                })
                .catch(error => {
                    console.log(error.response.data);
                });
            },
    },

});
